(function () {

var $modal = $('#pop-modal'); //弹出窗口元素
var oMenuData = []; //区域数据
var iProvinceIndex,iCityIndex; 
	 
	 function getPageData (Filter)
	 {
	 	var Filter = Filter;
	 		Filter.page = page.page;
	 		Filter.pageSize = page.pageSize;
	 	getJson(Domain+'/ec/usedTradingBase/doSearchTradingBaseList.do', Filter, function (res) {

		  if(!res.success){
	
		    $('.am-modal-bd').text(res.errorMessage);
				$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);

		  }
		  else{

	  	 	 $('#totalnum').text(res.data.rowCount);	

	  		  page.nowPage = res.data.pageId;
      		  page.pageCount = res.data.pageCount;
      		  
      		  if(!res.data.rows.length){
		        $('.am-pagination').remove();
		        $('#jidilist').append('<li class="empty_div">没有相关的商品哦~</li>');
		        return
		      }

		      console.log(res);

		     $('#jidilist').empty();

			 renderTmp('#jidilist', 'jdlisttpl',res);
			 	
			 	lazyLoad.refresh(true);

			 	$('.am-pagination').createPage({
			        current: page.page,
			        type: 1,
			        pageCount: page.pageCount,
			        center: 3,
			        showLen: 11,
		        callback: function (curidx, obj) {
		          page.page = curidx;
		          $('#jidilist').empty();
		          getPageData(Filter);
		        }
		       });

			 	$('.textpage').createPage({
			        current: page.page,
			        type: 2,
			        pageCount: page.pageCount,
		        callback: function (curidx, obj) {
		          page.page = curidx;
		          $('#jidilist').empty();
		          getPageData(Filter);
		        }
		       });
		  }
		},function (error) {

			$('.am-modal-bd').text(language.ajaxFailed);
							$modal.modal();
						setTimeout(function () {
							$modal.modal('close');
						},iWiating);

		})
	 }


	 //获取层级
	 laodAreas ({});
	 
	 function laodAreas (Filter)
	 {
	 	getJson(Domain+'/common/doLoadAllArea.do', Filter, function (res) {

		  if(!res.success){
	
		    $('.am-modal-bd').text(res.errorMessage);
				$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);

		  }
		  else{

		  	for (var i = 0; i< res.data.length; i++)
		  	{
		  		$('#provicelist').append('<option value="'+res.data[i].value+'">'+res.data[i].text+'</option>');
		  	}

		  	oMenuData = res.data;

	  	 	 createCityList(0,oMenuData);

	  	 	 getPageData ({
	  	 	 	areaId:0
	  	 	 }); //获取默认area ID 0的相关数据

		  }
		},function (error) {

			$('.am-modal-bd').text(language.ajaxFailed);
							$modal.modal();
						setTimeout(function () {
							$modal.modal('close');
						},iWiating);

		})
	 }

	//生成对应城市层级
	function createCityList(index,data)
	{
		$('#citylist').empty();

		for (var i = 0 ; i < data[index].children.length; i++)
	  	{
	  		$('#citylist').append('<option value="'+data[index].children[i].value+'">'+data[index].children[i].text+'</option>');
	  	}

		 createAreaList(data[index].children[0]);	
	}

	//生成对应区域数据
	function createAreaList(data)
	{
		$('#arealist').empty();

		for (var i = 0 ; i < data.children.length; i++)
		  	{
		  		$('#arealist').append('<option value="'+data.children[i].value+'">'+data.children[i].text+'</option>');
		  	}
	}

	//省份市级联动
	$('#provicelist').on('change',function () {

		iProvinceIndex = $(this).find('option').index($(this).find('option:selected'));

		createCityList(iProvinceIndex,oMenuData);

		getPageData ({
	  	 	 	areaId:$(this).find('option:selected').attr('value')
	  	 	 }); //查询下拉省份数据

	});

	//市级区域联动
	$('#citylist').on('change',function () {

		iCityIndex = $(this).find('option').index($(this).find('option:selected'));

		createAreaList(oMenuData[iProvinceIndex].children[iCityIndex]);

		getPageData ({
	  	 	 	areaId:$(this).find('option:selected').attr('value')
	  	 	 });//查询下拉城市数据

	});

	//区域查询
	$('#arealist').on('change',function () {

		getPageData ({
	  	 	 	areaId:$(this).find('option:selected').attr('value')
	  	 	 });//查询下拉区域数据

	});

})();